﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;

namespace shopsoft.Common
{
    class XmlProvider
    {
        protected string strXmlFile;
        protected XmlDocument xmlDoc = new XmlDocument();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="XmlPath"></param>
        public XmlProvider(string XmlPath)
        {
            // TODO: Complete member initialization
            try
            {
                this.xmlDoc.Load(XmlPath);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            this.strXmlFile = XmlPath;
        }

        /// <summary>
        /// 获取节点
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public XmlNode GetNode(string strPath)
        {
            return this.xmlDoc.SelectSingleNode(strPath);
        }

        /// <summary>
        /// 获取指定节点路径的指定属性值
        /// </summary>
        /// <param name="strNode"></param>
        /// <param name="strAttribute"></param>
        /// <returns></returns>
        public string GetNodeAttributeValue(string strNode, string strAttribute)
        {
            string str = "";
            try
            {
                XmlNode node = this.GetNode(strNode);
                if (node == null)
                {
                    return str;
                }
                XmlAttributeCollection attributes = node.Attributes;
                for (int i = 0; i < attributes.Count; i = (int)(i + 1))
                {
                    if (!(attributes.Item(i).Name != strAttribute))
                    {
                        return attributes.Item(i).Value;
                    }
                }
            }
            catch
            {
            }
            return str;
        }


        /// <summary>
        /// StrXmlFile
        /// </summary>
        public string StrXmlFile
        {
            get
            {
                return this.strXmlFile;
            }
            set
            {
                this.strXmlFile = value;
            }
        }

        /// <summary>
        /// XmlDoc
        /// </summary>
        public XmlDocument XmlDoc
        {
            get
            {
                return this.xmlDoc;
            }
            set
            {
                this.xmlDoc = value;
            }
        }
    }
}
